<?php

declare(strict_types=1);

namespace App\Overwrites;

use DateTimeInterface;
use Hyperf\Database\Model\Model;
use Hyperf\Database\Model\ModelNotFoundException;

class MineModel extends \Mine\MineModel
{
    public const DISABLE = 0;

    public function getPerPage(): int
    {
        return (int)request('per_page', parent::getPerPage());
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        if ($date->getTimestamp() === 0) {
            return '';
        }

        return parent::serializeDate($date);
    }

    /**
     * 从缓存中获取模型不存在抛出失败
     * @param mixed $id
     * @return Model|null
     */
    public static function findFromCacheOrFail($id): ?Model
    {
        $model = parent::findFromCache($id);
        if (is_null($model)) {
            throw (new ModelNotFoundException())->setModel(get_called_class(), $id);
        }

        return $model;
    }
}
